﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Guess
{
    public partial class GuessAdd : System.Web.UI.Page
    {
        protected int guessType = 0;
        protected int userID = 28;//暂时测试用户28
        int guessID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string struserIDFromCookie = Common.Cookie.getCookie("userID");
            if (string.IsNullOrEmpty(struserIDFromCookie))
            {
                Response.Redirect("/");
            }
            else
            {
                userID = int.Parse(struserIDFromCookie);
            }

            if (!string.IsNullOrEmpty(Request.Form["HidGuessType"]))  
            {
                guessType = Convert.ToInt32(Request.Form["HidGuessType"]);        //竞猜类型
                Model.User_Guess_Main guess = new Jy123.Model.User_Guess_Main();  

               

                //竞猜类型1
                if (guessType == 1)
                {
                    guess.FType = guessType;
                    guess.FUser = userID;
                    guess.Title = Request.Form["txtTitle"];
                    guess.Point = Convert.ToInt32(Request.Form["point"]);
                    guess.Share = Convert.ToInt32(Request.Form["share"]);
                    guess.Account = Request.Form["txtAccount"];
                    guess.GuessDays = Convert.ToInt32(Request.Form["guessdays"]);
                    guess.SettlementDays = Convert.ToInt32(Request.Form["settlementdays"]);
                    guess.OpenTime = DateTime.Now;
                    guess.OverTime = guess.OpenTime;
                    guessID = new BLL.User_Guess_Main().Add(guess);
                    if (guessID > 0)
                    {
                        for (int i = 1; i < 21; i++)
                        {
                            Model.User_Guess_Item item = new Jy123.Model.User_Guess_Item();
                            if (!string.IsNullOrEmpty(Request.Form["txtitem" + i]))
                            {
                                item.FGuess = guessID;
                                item.Item = Request.Form["txtitem" + i];
                                if (i == Convert.ToInt32(Request.Form["right"]))
                                {
                                    item.IsRight = true;
                                }
                                new BLL.User_Guess_Item().Add(item);
                            }
                        }

                        Response.Redirect("/Guess/GuessList.aspx?type=MyCreate");//提交成功跳转到我的竞猜页
                    }
                   
                }

                //竞猜类型2
                if (guessType == 2)
                {
                   
                    guess.FType = guessType;
                    guess.FUser = userID;
                    guess.Title = Request.Form["txtTitle"];
                    guess.Point = Convert.ToInt32(Request.Form["point"]);
                    guess.Share = Convert.ToInt32(Request.Form["share"]);
                    guess.Multiple = Convert.ToInt32(Request.Form["multiple"]);
                    guess.SumPoint = Convert.ToInt32(Request.Form["maxshare"]) * guess.Point;
                    guess.Account = Request.Form["txtAccount"];
                    guess.GuessDays = Convert.ToInt32(Request.Form["guessdays"]);
                    guess.SettlementDays = Convert.ToInt32(Request.Form["settlementdays"]);
                    guess.OpenTime = DateTime.Now;
                    guess.OverTime = guess.OpenTime;
                    guessID = new BLL.User_Guess_Main().Add(guess);
                    if (guessID > 0)//是否添加成功
                    {
                        Model.User_Main user = new BLL.User_Main().GetModel(userID);
                        user.Cash = user.Cash - guess.SumPoint;//扣除预扣现金
                        new BLL.User_Main().Update(user);

                        //添加竞猜项到数据库
                        for (int i = 1; i < 21; i++)
                        {
                            Model.User_Guess_Item item = new Jy123.Model.User_Guess_Item();
                            if (!string.IsNullOrEmpty(Request.Form["txtitem" + i]))
                            {
                                item.FGuess = guessID;
                                item.Item = Request.Form["txtitem" + i];
                                if (i == Convert.ToInt32(Request.Form["right"]))
                                {
                                    item.IsRight = true;
                                }
                                new BLL.User_Guess_Item().Add(item);
                            }
                        }

                        Response.Redirect("/Guess/GuessList.aspx?type=MyCreate");//提交成功跳转到我的竞猜页
                    }

                }
            }
            
            if (!string.IsNullOrEmpty(Request.QueryString["guesstype"]))
            {
                guessType = Convert.ToInt32(Request.QueryString["guesstype"]);           
            }
            PageLoad();
        }

        /// <summary>
        /// 获取竞猜类型html
        /// </summary>
        /// <returns>htmlString</returns>
        protected string getGuessTypeList()
        {
            StringBuilder sbHtml = new StringBuilder(Cache["guess_typelist"].ToString()); //竞猜类型列表

            sbHtml = sbHtml.Replace("$GuessTypeUl$", getGuessTypeUl());

            return sbHtml.ToString();
        }

        /// <summary>
        /// 获取竞猜类型UL
        /// </summary>
        /// <returns>ULString</returns>
        protected string getGuessTypeUl()
        {
            StringBuilder sbHtml = new StringBuilder();

            string strtypeul = Cache["guess_typeul"].ToString(); //竞猜类型UL

            BLL.User_Guess_Type bllGuessType = new Jy123.BLL.User_Guess_Type();
            List<Jy123.Model.User_Guess_Type> TypeList = bllGuessType.GetModelList("");

            int i = 1;
            foreach (Jy123.Model.User_Guess_Type type in TypeList)
            {
                string str = strtypeul;
                str = str.Replace("$Count$", i.ToString());
                str = str.Replace("$Value$", type.ID.ToString());
                str = str.Replace("$GuessTitle$", type.TypeName);
                str = str.Replace("$GuessAccount$", type.TypeAccount);
                i++;
                sbHtml.Append(str);
            }

            return sbHtml.ToString();

        }

        /// <summary>
        /// 加载页面
        /// </summary>
        protected void PageLoad()
        {
            Model.User_Main userModel = new BLL.User_Main().GetModel(userID);
            StringBuilder sbHtml = new StringBuilder(Cache["guess_index"].ToString());//主sb

            //公共部分
            if (guessType == 0)
            {
                sbHtml = sbHtml.Replace("$Javascript$", Cache["guess_js"].ToString());//JS
            }
            else
            {
                sbHtml = sbHtml.Replace("$Javascript$", Cache["guess_js"].ToString() + Cache["guess_addjs"].ToString());//JS
                sbHtml = sbHtml.Replace("$GuessCash$", userModel.Cash.ToString());//现金
            }
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$Navigation$", Cache["user_navigation_down"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧快速添加好友
            sbHtml = sbHtml.Replace("$GuessLogo$", Cache["guess_logo"].ToString());//竞猜LOGO
            sbHtml = sbHtml.Replace("$GuessMenu$", Cache["guess_menu"].ToString());//竞猜菜单
            sbHtml = sbHtml.Replace("$IndexClass$", "")
               .Replace("$AddClass$", "class=\"sel\"")
               .Replace("$FriendsClass$", "")
               .Replace("$MyOriClass$", "")
               .Replace("$MyParClass$", "")
               .Replace("$TopClass$", "class=\"last\"")
               .Replace("$Cash$", Convert.ToInt32(userModel.Cash).ToString("n0"));

            if (guessType == 0)
            {
                sbHtml = sbHtml.Replace("$Content$", getGuessTypeList());//竞猜类型列表
            }
            if (guessType == 1)
            {
                sbHtml = sbHtml.Replace("$Content$", getGuessAdd());//竞猜类型1竞猜信息
            }
            if (guessType == 2)
            {
                sbHtml = sbHtml.Replace("$Content$", getGuessAdd());//竞猜类型2竞猜信息
            }
            Response.Write(sbHtml.ToString());

        }

        /// <summary>
        /// 获取添加竞猜信息HTML
        /// </summary>
        /// <returns>HTMLString</returns>
        protected string getGuessAdd()
        {
            StringBuilder sbHtml = new StringBuilder(Cache["guess_add"].ToString());

            if (guessType == 1)
            {
                sbHtml.Replace("$InputHid$", " class=\"hid\"");       //竞猜类型1隐藏控件
        
            }
            else
            {
                sbHtml.Replace("$InputHid$", "");
            }
            sbHtml.Replace("$GuessType$", guessType.ToString());
            return sbHtml.ToString();
        }
        
        
    }
}
